from typing import Any, Optional
from fastapi import HTTPException

from app.core.i18n import t


class APIException(Exception):
    """API异常"""

    def __init__(
        self,
        message: str = t("response.fail"),
        data: Optional[Any] = {},
        code: Optional[str] = 0,
        status_code: int = 200,
    ):
        self.status_code = status_code
        self.message = message
        self.data = data
        self.code = code

    def __str__(self):
        return f"APIException: {self.message}"

    def __repr__(self):
        return f"APIException: {self.message}"

    def __dict__(self):
        return {
            "status_code": self.status_code,
            "message": self.message,
            "data": self.data,
            "code": self.code,
        }


class ServiceException(APIException):
    """服务异常"""

    def __init__(
        self,
        message: str = t("response.fail"),
        data: Optional[Any] = {},
        code: Optional[str] = 0,
        status_code: int = 200,
    ):
        super().__init__(message, data, code, status_code)


class NotFoundException(HTTPException):
    def __init__(self, detail: str):
        super().__init__(status_code=404, detail=detail)


class ValidationException(HTTPException):
    def __init__(self, detail: str):
        super().__init__(status_code=422, detail=detail)
